package com.gs.xbinfomytest.service.impl;

import com.gitee.hengboy.mybatis.enhance.dsl.factory.EnhanceDslFactory;
import com.gitee.hengboy.mybatis.enhance.dsl.serach.Searchable;
import com.gitee.hengboy.mybatis.enhance.sort.SortEnum;
import com.gs.xbinfomytest.dto.QueryUserDto;
import com.gs.xbinfomytest.dto.TableDataDto;
import com.gs.xbinfomytest.entity.SysMenu;
import com.gs.xbinfomytest.entity.SysUser;
import com.gs.xbinfomytest.entity.dsl.DSysMenu;
import com.gs.xbinfomytest.entity.dsl.DSysRoleMenu;
import com.gs.xbinfomytest.entity.dsl.DSysUser;
import com.gs.xbinfomytest.entity.dsl.DSysUserRole;
import com.gs.xbinfomytest.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Created : IntelliJ IDEA.
 * @ProjectName: xbinfomytest
 * @Package: com.gs.xbinfomytest.service.impl
 * @Author: guoshuai
 * @CreateDate: 2019/8/20 13:44
 * @Description: java类的作用描述
 * @Vsersion: 1.0
 */
@Service("userService")
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {

    @Autowired
    private EnhanceDslFactory dslFactory;

    @Override
    public TableDataDto queryByPage(QueryUserDto queryUserDto) {
        TableDataDto tableDataDto = new TableDataDto();
        DSysUser dSysUser = DSysUser.DSL();
        try {
            Long count = 0L;
            Integer pageinfo = queryUserDto.getPage();
            if (queryUserDto.getPage() == 0) {
                pageinfo = 1;
            }
            Integer offset = (pageinfo - 1) * (queryUserDto.getLimit());
            Searchable searchableDto = dslFactory.createSearchable()
                    .selectFrom(dSysUser)
                    .and(" 1=1 ");
            Searchable searchableCount = dslFactory.createSearchable()
                    .count(dSysUser.userId).from(dSysUser)
                    .and(" 1=1 ");

            if (!StringUtils.isEmpty(queryUserDto.getUserName())) {
                searchableDto.and(dSysUser.loginName.like("%", queryUserDto.getUserName(), "%"));
                searchableCount.and(dSysUser.loginName.like("%", queryUserDto.getUserName(), "%"));
            }

            //查询个数
            count = searchableCount.resultType(Long.class).fetchOne();
            List<SysUser> sysUserList = searchableDto.orderBy(dSysUser.createTime, SortEnum.ASC)
                    .offset(offset)
                    .limit(queryUserDto.getLimit())
                    .resultType(SysUser.class)
                    .fetch();
            tableDataDto.setTotal(count);
            tableDataDto.setMsg("查询成功！");
            tableDataDto.setCode(0);
            tableDataDto.setRows(sysUserList);
        } catch (Exception e) {
            e.printStackTrace();
            tableDataDto.setTotal(Long.valueOf("0"));
            tableDataDto.setMsg("查询失败！");
            tableDataDto.setCode(500);
            tableDataDto.setRows(new ArrayList<SysUser>());
        }
        return tableDataDto;
    }

    @Override
    public SysUser finUserInfoByUserId(String userId) {

        return null;
        // Map<String, Object> params = new HashMap<>();
        // String sql=" from oauth2_user where id=:id";
        //  return userMapper.selectOne(userId);
    }

    @Override
    public void saveUserInfo(SysUser user) {
        // userMapper.insert(user);
    }

    @Override
    public SysUser findByUserNameAndPassword(String username, String password) {
        DSysUser dSysUser = DSysUser.DSL();
        SysUser sysUser = dslFactory.createSearchable().selectFrom(dSysUser)
                .where(dSysUser.loginName.eq(username))
                .and(dSysUser.password.eq(password))
                .resultType(SysUser.class)
                .fetchOne();
        return sysUser;
    }

    @Override
    public List<SysMenu> findByUserId(Long userId) {
        DSysMenu dSysMenu = DSysMenu.DSL();
        // DSysUser dSysUser = DSysUser.DSL();
        DSysUserRole dSysUserRole = DSysUserRole.DSL();
        DSysRoleMenu dSysRoleMenu = DSysRoleMenu.DSL();
        List<SysMenu> mylist = dslFactory.createSearchable().selectFrom(dSysMenu)
                .leftJoin(dSysMenu.menuId, dSysRoleMenu.menuId)
                .leftJoin(dSysRoleMenu.roleId, dSysUserRole.roleId)
                .where(dSysUserRole.userId.eq(userId))
                .orderBy(dSysMenu.orderNum, SortEnum.ASC)
                .resultType(SysMenu.class)
                .fetch();
        return mylist;
    }

}
